<?php

namespace App\Http\Controllers\Admin\Category;


use App\Models\Category\Category;
use App\Services\CategoryService;
use App\Transformers\Category\ConfigCategoryShowTransformer;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\HttpException;


class ConfigCategoryController extends BaseController
{
    /**
     * 分类的组名
     */
    public $type = 'config';

    public function show($id, Request $request)
    {
        $category = $this->categoryService->getCacheCategory($id);

        if (empty($category)) {
            return $this->response->errorNotFound();
        }

        return $this->response->item($category, new ConfigCategoryShowTransformer());
    }

    //获得分类简单数据
    public function config_name()
    {
        $data = Category::query()
            ->where('category_group_id', $this->categoryService->category_group_id)
            ->select('id', 'id as value', 'nickname', 'pid')
            ->orderBy('id')
            ->pimp()
            ->get()->toArray();

        // 生成树
        $tree = generateTree(objectToArray($data), 'value');

        return $this->response->array($tree);
    }

    /**
     * 状态禁用和启用
     */
    public function status(Category $category)
    {
        $category->status = $category->status == 0 ? 1 : 0;
        $category->save();

        $this->categoryService->delCacheCategory($category->id);

        return $this->response->noContent();
    }

    public function rules(Request $request): array
    {
        $categoryService = new CategoryService(request(), $this->type);

        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
                return [
                    //bail在首次验证失败后立即终止验证。
                    'name' => ['bail', 'nullable', 'string', 'max:32'],
                    'nickname' => ['bail', 'required', 'string', 'max:50', function ($attr, $value, $fail) use ($categoryService) {

                        //属性的名称、属性的值以及$fail验证失败时应调用的回调
                        $result = $categoryService->checkCategoryNickname(request()->input('pid', null), $value);

                        if (!$result) {
                            return $fail('昵称重复');
                        }
                    }],
                    'pid' => 'required|integer',
                    'image' => 'image ',
                ];
                break;
            case 'PATCH':
            case 'PUT':
                //从服务器中拿到id
                $id = route_parameter($this->type.'_category');

                if (empty($id)){
                    $id = route_parameter('id');
                }

                $category = $categoryService->getCacheCategory($id);

                if (empty($category)) {
                    throw new HttpException(404,'ID或路由错误');
                }

                return [
                    'name' => ['bail', 'nullable', 'string', 'max:32'],
                    'nickname' => ['bail', 'string', 'max:50', function ($attr, $value, $fail) use ($categoryService,$id) {
                        $result = $categoryService->checkCategoryNickname(request()->input('pid', null), $value, $id);
                        if (!$result) {
                            return $fail('昵称重复');
                        }
                    }],
                    'pid' => 'nullable|integer'
                ];
                break;
        }
        return [];
    }

}
